#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <iomanip>

struct Process {
    std::string name;
    int arrivalTime;
    int serviceTime;
    int startTime;
    int endTime;
    float turnaroundTime;
    float weightedTurnaroundTime;
};

void calculateTimes(std::vector<Process>& processes) {
    int currentTime = 0;
    for (auto& process : processes) {
        if (currentTime < process.arrivalTime) {
            currentTime = process.arrivalTime;
        }
        process.startTime = currentTime;
        process.endTime = process.startTime + process.serviceTime;
        process.turnaroundTime = process.endTime - process.arrivalTime;
        process.weightedTurnaroundTime = process.turnaroundTime / process.serviceTime;
        currentTime = process.endTime;
    }
}

int main() {

    std::cout << "<<----------先来先服务算法---------->>\n" << std::endl;

    int n;
    std::cout << "请输入进程数目: ";
    std::cin >> n;

    std::vector<Process> processes(n);
    std::cout << "请依次输入各个进程的信息（格式：进程名称 到达时间 服务时间）：" << std::endl;
    for (int i = 0; i < n; ++i) {
        std::cout << "请输入进程" << i + 1 << "的信息: ";
        std::cin >> processes[i].name >> processes[i].arrivalTime >> processes[i].serviceTime;
    }

    calculateTimes(processes);

    std::cout << "\n调用先来先服务算法以后进程的运行顺序为：";
    for (const auto& process : processes) {
        std::cout << process.name;
        if (&process != &processes.back()) {
            std::cout << " --> ";
        }
    }
    std::cout << std::endl;

    std::cout << "\n具体的进程调度信息：\n";

    std::cout << "进程名\t到达时间\t服务时间\t开始时间\t结束时间\t周转时间\t带权周转时间" << std::endl;
    float totalTurnaroundTime = 0, totalWeightedTurnaroundTime = 0;
    for (const auto& process : processes) {
        std::cout << process.name << "\t"
            << std::fixed << std::setprecision(3) << process.arrivalTime << "\t\t"
            << std::fixed << std::setprecision(3) << process.serviceTime << "\t\t"
            << std::fixed << std::setprecision(3) << process.startTime << "\t\t"
            << std::fixed << std::setprecision(3) << process.endTime << "\t\t"
            << std::fixed << std::setprecision(3) << process.turnaroundTime << "\t\t"
            << std::fixed << std::setprecision(3) << process.weightedTurnaroundTime << std::endl;
        totalTurnaroundTime += process.turnaroundTime;
        totalWeightedTurnaroundTime += process.weightedTurnaroundTime;
    }

    std::cout << "\n平均周转时间为: " << std::fixed << std::setprecision(3) << (totalTurnaroundTime / n) << std::endl;
    std::cout << "平均带权周转时间为: " << std::fixed << std::setprecision(3) << (totalWeightedTurnaroundTime / n) << std::endl;

    return 0;
}



